#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct employee {
    char name[10];
    int salary;
};

int main() {
    FILE *fp;
    int i, j, n, flag;
    char name[10];

    if ((fp = fopen("d:\\emp_salary", "rb")) == NULL) {
        printf("can not open file.\n");
        exit(0);
    }

    // Read and output the contents of the file
    printf("original data:\n");
    for (i = 0; fread(&emp[i], sizeof(struct employee), 1, fp) != 0; i++) {
        printf("%8s %7d\n", emp[i].name, emp[i].salary);
    }
    fclose(fp);
    n = i;

    printf("\ninput name to be deleted:\n");
    scanf("%s", name);

    flag = 1;
    for (i = 0; flag && i < n; i++) {
        if (strcmp(name, emp[i].name) == 0) {
            for (j = i; j < n - 1; j++) {
                strcpy(emp[j].name, emp[j + 1].name);
                emp[j].salary = emp[j + 1].salary;
            }
            flag = 0;
        }
    }

    if (!flag) {
        n = n - 1;
    } else {
        printf("\nnot found!");
    }

    printf("\nNow, the content of file:\n");
    if ((fp = fopen("d:\\emp_salary", "wb")) == NULL) {
        printf("can not open file\n");
        exit(0);
    }

    for (i = 0; i < n; i++) {
        fwrite(&emp[i], sizeof(struct employee), 1, fp);
    }
    fclose(fp);

    fp = fopen("d:\\emp_salary", "r");
    for (i = 0; fread(&emp[i], sizeof(struct employee), 1, fp) != 0; i++) {
        printf("%8s %7d\n", emp[i].name, emp[i].salary);
    }
    fclose(fp);

    return 0;
}